package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.adapter.ConstructPlanOrContentAdapter;
import com.cloudsigns.factorymanager2.engineering.adapter.EditConstructContentAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructContentBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 工单管理 编辑工单 工程施工 编辑施工内容
 * Created by tony on 2018/1/29.
 */

public class EditConstructContentActivity extends Activity implements View.OnClickListener, ItemRemoveListener, EditConstructContentAdapter.TextChangeLiastener {
    private ImageView img_back;
    private RelativeLayout rl_right;
    private TextView tv_title;
    private ListView lv;
    private ImageView img_edit_construct_content;
    private TextView btn_upload;
    private EditConstructContentAdapter adapter;
    private List<EngineeringConstructContentBean> datas = new ArrayList<>();
    private final String TAG = EditConstructContentActivity.class.getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_edit_construct_content);


        init();
    }

    private void init() {
        img_back = findViewById(R.id.img_back);
        rl_right=findViewById(R.id.rl_right);
        tv_title = findViewById(R.id.tv_title);
        btn_upload = findViewById(R.id.btn_upload);
        lv = findViewById(R.id.lv);
        img_edit_construct_content = findViewById(R.id.img_edit_construct_content);


        tv_title.setText(getResources().getText(R.string.edit_engineering_construct_content));
        img_back.setOnClickListener(this);
        btn_upload.setOnClickListener(this);
        img_edit_construct_content.setOnClickListener(this);
        rl_right.setOnClickListener(this);

        if (getIntent() != null) {
            datas = (List<EngineeringConstructContentBean>) getIntent().getSerializableExtra(ConstantUtil.PRODUCTBEAN);
        }

//        datas.add(new EngineeringConstructContentBean("现场安装1", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装2", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装3", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装4", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装5", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装6", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装7", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装8", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装9", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装10", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装11", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装12", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装13", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装14", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装15", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装16", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装17", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装18", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装19", "", ""));
//        datas.add(new EngineeringConstructContentBean("现场安装20", "", ""));


        adapter = new EditConstructContentAdapter(this, TAG);
        adapter.setData(datas);
        adapter.setItemRemoveListener(this);
        lv.setAdapter(adapter);

    }


    @Override
    public void remove(int positon) {
        if (positon >= 0 && positon < datas.size()) {
            datas.remove(positon);
            adapter.setData(datas);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void remove(View view) {

    }

    @Override
    public void textChange(int posittion, String content) {
//        datas.get(posittion).setContent(content);
    }

    /**
     * 添加工程施工内容
     */
    private void addEngineeringConstructContent() {
//        datas.add(new EngineeringConstructContentBean("", "", ""));
        adapter.setData(datas);
        adapter.notifyDataSetChanged();
    }

    private void upload() {
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.PRODUCTBEAN, (Serializable) datas);

        setResult(ConstantUtil.ADD_PRODUCT_INFOMATION, mIntent);
        Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show();

        finish();
    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.rl_right:
                mIntent=new Intent(this,EditConstructContentMoreActivity.class);
                startActivity(mIntent);
                break;
            case R.id.btn_upload:
                upload();
                break;
            case R.id.img_edit_construct_content:
                addEngineeringConstructContent();
                break;

        }
    }

}
